/*
 *  $TEAM_NAME Space Trader
 */
package presenters;

import models.Galaxy;
import models.Game;
import models.Player;

import views.HubPanel;
import views.NewGamePanel;
import views.TitlePanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;

/**
 * Handles the presentation of the new game view to the palayer
 * @author bradley
 * @version 1.0
 */
public class NewGamePresenter extends Presenter<NewGamePanel> 
implements ActionListener, ChangeListener {
	/**
	 * field
	 */
	private static final int MAX_POINTS = 16;

    /**
     * Default constructor for the new game presenter
     * @param view - the new game view
     */
    public NewGamePresenter(NewGamePanel view) {
       // Set the view to present
       view.addActionListener(this);
       view.addChangeListener(this);
       super.setView(view);
    }
    
    /**
     * Handles the action of when the player presses 
     * the next button on the new game view
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        final Object source = e.getSource();

        if (source instanceof JButton) {
            // Button code
            final JButton button = (JButton) source; 

            if ("Next".equals(button.getText())) {
                final String name = view.getName();

                if (remainingPoints() == 0) {
                    if (name.length() > 0) {
                        // Make the player
                        final Player player = Player.getPlayer();

                    	player.setName(view.getName());
                    	player.setPilot(view.getPilot());
                    	player.setTrader(view.getTrader());
                    	player.setFighter(view.getFighter());
                    	player.setEngineer(view.getEngineer());

                    	// Make a new galaxy
                        final Galaxy galaxy = Galaxy.getGalaxy();
                        player.setLocation(galaxy.getStarSystems().get(0));

                        // Make a new game
                        final Game game = new Game(galaxy, player, view.getDifficulty()); 
                        Game.save(game);

                        // Transition to the hub
                        final HubPresenter hub = new HubPresenter(
                        		game, new HubPanel(game, this.getFrame()));
               
                        present(hub);
                    } else {
                        // Enter a valid name
                        JOptionPane.showMessageDialog(frame, "Enter a name.");
                        System.out.println("Enter a name.");
                    }
                } else {
                    // Enter the corrent number of points
                    JOptionPane.showMessageDialog(frame, "Check your points.");
                    System.out.println("Check your points.");
                }
            } else if ("Back".equals(button.getText())) {
                final TitlePanel titlePanel = new TitlePanel();
                final TitlePresenter titlePresenter = new TitlePresenter(titlePanel);
                
                present(titlePresenter);
            }
        }
    }

    /**
     * Updates the slider to allocate points
     * @param ce - change event of slider change
     */
    public void stateChanged(ChangeEvent ce) {
        if(remainingPoints() < 0){
        	final JSlider slider = (JSlider) ce.getSource();
        	slider.setValue(slider.getValue() - 1);
        }
    	
    	view.setRemainingPoints(remainingPoints());
    }

    /**
     * Calculates the points left to allocate
     * @return - points left
     */
    public int remainingPoints() {
        int pointsAllocated = 0;

        pointsAllocated += view.getFighter();
        pointsAllocated += view.getEngineer();
        pointsAllocated += view.getTrader();
        pointsAllocated += view.getPilot();

        return MAX_POINTS - pointsAllocated;
    }
    
    /**
     * string
     * @return string
     */
    public String toString() {
		return "";
	}
}
